package com.ross.mode.proxy3;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Date;

/**
 * @version 1.0.0
 * @className: UserProxy
 * @description: JDK动态代理代理类
 * @author: zhangjunfa
 * @date: 2023/3/31 16:04
 */
@Slf4j
public class UserProxy implements InvocationHandler {

    private Object target;

    public UserProxy() {
    }

    /**
     * 绑定委托对象并返回一个代理类
     *
     * @param target
     * @return
     */
    public Object newInstance(Object target) {
        this.target = target;
        //取得代理对象
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        log.info("执行方法前-==={}", new Date());
        Object result = method.invoke(target, args);
        log.info("执行方法后-==={},参数：{}，方法：{}", new Date(), args, method.getName());
        return result;

    }
}
